//
//  Dog.m
//  复习2
//
//  Created by Justsoso on 16/3/12.
//  Copyright © 2016年 Justsoso. All rights reserved.
//




#import "Dog.h"

@implementation Dog

- (instancetype)initWithName:(NSString *)name
{
    self = [super init];
    if (self) {
        self.hungry = 100;
        self.die = NO;
        self.firstName = name;
        [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(ishungry:) userInfo:nil repeats:YES];
    }
    return self;
}

- (void)ishungry:(NSTimer *)time
{
    self.hungry -= 10;
    if (self.hungry == 0)
    {
        self.die = YES;
    }    
    NSDictionary *dic = @{@"饥饿度":@(self.hungry)};
    NSLog(@"%@:我饿了  饥饿度:%i",self.fullName,self.hungry);
    if (self.die == YES)
    {
        NSLog(@"%@:我饿死了！！",self.firstName);
        [time invalidate];
    }
    if (self.hungry < 60)
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"hungry" object:self userInfo:dic];
    }
    if (self.hungry <40)
    {
        [self.delegate feed];
    }
}

- (void)setFirstName:(NSString *)firstName
{
    _firstName = firstName;
    NSLog(@"主人你好!我叫%@，请多关照!",firstName);
}

- (NSString *)lastName
{
    if (_lastName == nil)
    {
        return @"华盛顿";
    }
    return _lastName;
}

- (NSString *)fullName
{
    NSString *full = [NSString stringWithFormat:@"%@.%@",self.firstName,self.lastName];
    return full;
}

@end
